package com.yc.forum.servlet;

import com.yc.forum.dao.RedisDao;
import com.yc.forum.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用户收藏文章
 *  1.文章收藏数量加一
 *  2.将文章id存入用户收藏夹
 *  3.文章发布者积分加16
 */
@WebServlet(name="CollectionPostServlet",value="/CollectionPostServlet.s")
public class CollectionPostServlet extends HttpServlet {
    RedisDao redisDao=new RedisDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        //取值
        String postId=req.getParameter("postId");
        String nowCollectionSum=req.getParameter("sum");
        //捕获转型异常
        try{
            //获取当前登录账号
            String account=session.getAttribute("loginedUser").toString();

            //帖子收藏数量加一
            int endsum=Integer.parseInt(nowCollectionSum)+1;
            redisDao.addPostCollectionSumById(postId,endsum);
            //将文章id存入账号收藏集合
            redisDao.addCollectionPostId(postId,account);
            //为贴子发布者增加16点积分
            String postAccount=req.getParameter("postAccount");
            Utils.addReward(postAccount,16);
            //返回值
            Utils.toJson(1,resp);
        }catch(NumberFormatException e){
            System.out.println("转型错误"+e.toString());
            Utils.toJson(-1,resp);
        }
    }
}
